This page looks best with JavaScript enabled

Docker Commands & Configurations

 ·  ☕ 2 min read

Docker

Dockerfile

1
2
3
4
5
6
7
8
FROM <baseimage_name>:<baseimage_tag>
ADD <hpath> <abs_cpath>  # copy file/dir from abs. or rel. path on host to abs. path in container/image
WORKDIR <absolute_cpath>  # create and enter a dir in container/image
COPY <hpath> <rel_cpath>  # copy file/dir from abs. or rel. path on host to rel. path in container/image (rel. to WORKDIR)
ENV <envvar>  # sets environment variable (can be set by command in docker-compose.yml or run command)
EXPOSE <cport>  # exposes container port (can be set by command in docker-compose.yml or run command)
RUN <command + args>  # executes a command at image build
CMD [ "<command", "arg"] # executes a command at container run (can be overwritten by command in docker-compose.yml or run command)

Docker Commands

Build Image

1
2
$ docker image build --rm -t <image_name> -f <dockerfile_name> .  # if the file is called <dockerfile_name> (different from std name "Dockerfile"
$ docker image build --rm -t <image_name> .

Create Volume

… to do …

Run Container from Image

1
2
3
4
5
$ docker container run --rm     -p <cport>:<hport> --name <container_name> <image_name>
$ docker container run --rm -d  -p <cport>:<hport> --name <container_name> <image_name>       # start in the background
$ docker container run --rm -it -p <cport>:<hport> --name <container_name> <image_name>       # start interactive
$ docker container run --rm -it -p <cport>:<hport> --name <container_name> <image_name> bash  # start interactive and get a bash prompt
$ docker container run --rm -p <cport>:<hport> -v <hpath>:<abs_cpath> --name <container_name> <image_name>

Enter a Running Container Interactively

1
2
$ docker container exec -it  <container_id> bash
$ docker exec -it <container_id> redis-cli -a my-secret-password [-h 127.0.0.1 -p 6379]

Docker Compose

docker-compose.yaml

version: '3'

services:

  <service_name_1>:
    build: <rel_hpath>  # rel. path to directory where the Dockerfile is located
    image: <image_name_1>
    container_name: <container_name_1>
    ports:
      - "<cport>:<hport>"
    command: <command arg>

  <service_name_1>:
    image: <image_name_1>  # uses the same image that was created under firts service
    container_name: <container_name_2>  # can be ommitted then it will be given a name based on the service and image name
    ports:
      - "<cport>:<hport>"
    volumes:
      - <hpath>:<abs_cpath>  # a bind volume to the local filesystem
	environment:
      - <name>=<value>
    command: <command arg>
	depends_on:
      - <service_name_1>

Docker Commands Commands

1
2
3
4
$ docker-compose build
$ docker-compose up [--build] [<service_name>] [<service_name>]
$ docker-compose up -d --scale <service_name>=2
$ docker-compose down

Philipp Westphal
WRITTEN BY
Philipp Westphal
Software Developer & Structural Engineer