Another year, another euskalencounter.org 🙂 This year I’ve binge watched Altered Carbon, and katakoda’s Docker training, below is my cheat sheet for future reference.
- docker search java
- search for a docker image (java in this case)
- running a docker image
- docker run -d openjdk
- run detached (-d) the openjdk docker
- docker run -d –name redisHostPort -p 6379:6379 redis:latest
- –name creates a user friendly name for your running docker
- -p <machine host-port:container-port> exposes docker image port in the host port
- redis:latest is the docker image:the version
- docker run -d –name redisDynamic -p 6379 redis:latest
- -p 6379 assigns a random host port
- docker port redisDynamic 6379
- shows to which host ip:port is docker running image redisDynamic’s port 6379 is assigned
- docker run -d –name redisMapped -v /opt/docker/data/redis:/data redis
- -v host-path:docker-path maps a docker path to a host path in order to persist data
- docker run -it ubuntu bash
- -it runs an interactive docker image.
- you can run a specific command on the docker image (bash)
- docker run -d –name my-production-running-app -e NODE_ENV=production -p 3000:3000 my-nodejs-app
- -e VARIABLE=value sets an environment variable in the running container
- docker run -d openjdk
- How to run a command inside a running container
- docker exec -d ubuntu_bash touch /tmp/execWorks
- How to create docker data volumes
- Create a new container to start it afterwards (instead of running it directly) this is useful mainly to create data storage dockers and use them from other images.
- docker create -v /data –name dataContainer ubuntu
- docker run –rm –volumes-from dataContainer ubuntu ls -la /data
- docker cp config.conf dataContainer:/config/
- copies file config.com from the host to the docker container
- to move data containers from one machine to another you can export/import them to .tar
- docker export dataContainer > dataContainer.tar
- docker import dataContainer.tar
- docker import http://example.com/exampleimage.tgz
- How to create a network between containers
- docker network create backend-network
- creates a network to which we can attach containers. Docker sets a dns server in ip 127.0.0.11 and all connected containers use it by docker adding it to resolv.conf on each container
- docker network connect frontend-network redis
- connects a container to a network
- docker network connect –alias db frontend-network2 redis
- –alias <alias_name> creates an alias for the network name
- docker network ls
- shows all networks
- docker network inspect frontend-network
- shows all containers connected to a network and their ip addresses
- docker network disconnect frontend-network redis
- disconnect a container from a network
- docker run -d –name=redis –net=backend-network redis
- launches a redis container linked to the network backend-network.
- docker network create backend-network
- Building a docker image
- docker build -t webserver-image:v1 .
- -t <image name:tag> is used to give a name and version to the image.
- in the . directory you need to create a file called Dockerfile with yaml notation
- .dockerignore
- echo passwords.txt >> .dockerignore
- all fine names included into file .dockerignore will be ignored when building the image
- echo passwords.txt >> .dockerignore
- Dockerfile
- FROM nginx:alpine
- uses nginx:alpine as base for this docker image
- COPY . /usr/share/nginx/html
- copies from the host to the image
- FROM node:7-alpine
- RUN mkdir -p /src/app
- RUN’s command inside the docker image
- WORKDIR /src/app
- set the working directory in the docker image
- EXPOSE 3000
- EXPOSEs the port from the docker image to a random port in the host system
- CMD [ “npm”, “start” ]
- runs a command in the docker image you can add parameters as items of the array
- ONBUILD COPY . /usr/src/app
- onbuild commands won’t be executed until the built image is used as a base image
- FROM nginx:alpine
- docker images
- returns a list of images installed in the host system
- docker build -t webserver-image:v1 .
- removing a docker image
- docker container ls -a
- lists all containers (running or not)
- docker container rm < CONTAINER ID>
- removes the container
- docker rmi hello-world
- removes image hello-world
- docker container ls -a
- docker ps
- show docker processes
- docker inspect openjdk
- show details of running docker
- docker logs openjdk
- shows logs for running docker