Docker cheat sheet

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.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s