Basics

Basic Commands

​
#
Command
Description
1
docker build .
Build the image from the Dockerfile found in the project directory
2
docker run -p [port_no]:[port_no] [image_id]
Run built docker image with -p flag (to publish, open port so we can reach from outside) and use image id, for example: docker run -p 3000:3000 417916cb028c
3
docker ps
List all the containers
4
docker stop [container_name]
Stop given docker image, based on it's name, for example: docker stop relaxed_andrew
5
docker run [image_from_hub]
This command will run local image, if note found it will pull it from DockerHub, e.g. docker run node
6
docker ps -a
Show all processes (ps)
7
docker run -it node
Access interactive section of Node instance, where you can run basic commands, node api, etc.
8
docker run --help
Shows all available option for command docker run and explains all available flags.

Containers

#
Commmand
Description
1
docker run -p 8000:80 -d [image_id]
Run container in the detached mode, this doesn't block your terminal so you can run different commands, but you can't see the output.
2
docker attach [container_name]
You can switch on the attach mode, which will show you the p=output in the console, so you can see printed statments, etc. Example use: docker attach determined_swartz
3
docker logs [container_name]
See the container's logs (you can see past logs), but stay in detached mode. Example: docker logs feedbakc-app
4
docker logs -f [container_name]
See the logs and activate the attach mode (now you will be able to see the future logs).
5
docker start -a [container_name]
Start container in the attach mode (you can see output). Example use: docker start -a determined_swartz
6
docker run -i -t [image_id] or use docker run -it [image_id]
Run container in interactive mode which allows STDIN, you can provide input to the terminal window.
7
docker start -a -i [container_name]
Start the container in attach mode and with option allowing to input data in terminal (I want to input something mode).
8
docker rm [container_name]
Remove container that was previously stopped. You can pass multiple images names after rm, seperated by whitespace.
9
docker cp [source_dir_path] [container_name]:[path]
Allows to copy file into and from the container. Example use: docker cp dummy/. silly_ellis:/test - if /test directory doesn't exist it will be created. Dot (.) in the command in a path means all files in a given directory.
10
docker cp [container_name]:[path_to_file_or_folder_we_need] [local_folder]
Copy files from container to local folder, example use: docker cp silly_ellis:/test dummy
11
docker run -p [port]:[port] -d --rm --name [container_name] [image_id]
Name the container, example use: docker run -p 3000:80 -d --rm --name goalsapp 7764ae198515
12
docker run -p [local_port]:[docker_port] -d --rm --name [new_container_name] [image_repo_name]:[image_tag]
Run image based on name and tag, example use: docker run -p 3000:80 -d --rm --name goalsapp goals:latest

Images

#
Command
Description
1
docker images
List all images.
2
docker rmi [image_id]
Remove image and all its layers. Remember that container needs to be remove first for this command to work. Example use: docker rmi 430b0f5674df
3
docker image prune
Remove all unused images.
4
docker image prune -a
Remove all images, including tagged images.
5
docker run -p [port]:[port] -d --rm [image_id]
Run image on port 3000 in dettached mode and remove container when is being stopped. Example use: docker run -p 3000:80 -d --rm 430b0f5674df
6
docker image inspect [image_id]
It will output information about image in form of a json file.
7
docker build -t [name]:[tag] .
Tag the image with -t flag followed by name:tag (name is an image repo name, and tag is actual image name) - it can be a word or a number. Example use: docker build -t goals:latest .
8
docker tag [old_image_name]:[tag] [new_image_name]
Change the image name (it actually make a clone instead of changing name), for example: docker tag node:latest rectan/hello-world
9
docker login
Login to Docker Hub
10
docker logout
Logout from Docker Hub
11
docker push [repo_name]
Push image to your repo in Docker Hub, for example: docker push rectan/hello-world
12
docker pull [image_name]
Pull images from Docker Hub (this will detch the latest image), for example: docker pull rectan/hello-world
13
docker history [image_name]
This command shows the history of the image, what was done step by step and might expose some env variables. Example usage: docker history feedback-node:latest

Volumes and Bind Mounts

#
Command
Description
1
docker volume rm VOL_NAME
Remove a given volume, for example: docker volume rm feedback
2
docker volume prune
Remove all unused local volumes
3
docker run -d -p 3000:80 --rm --name feedback-app -v feedback:/app/feedback feedback-node:volumes
Run container in -d (detached mode), with -p on port 3000, --rm (remove container once stopped), --name (name it feedback-app) and attach the volume /app/feedback that should be mapped to feedback with -v flag, lastly create it from tag (feedback-node:volumes). This command allow you to persist the data, even after container was removed and re-created.
4
docker run -d -p 3000:80 --rm --name feedback-app -v feedback:/app/feedback -v "/Users/andrew/training/data-volumes-01-starting-setup:/app" feedback-node:volumes
Use bind mounts to map local folder on your machine to container app. In this way you can update code on your machine without re-creating the existing container. (shortcut for the full path: -v $(pwd):/app)
5
docker run -d -p 3000:80 --rm --name feedback-app -v feedback:/app/feedback -v "/Users/andrew/training/data-volumes-01-starting-setup:/app" -v /app/node_modules feedback-node:volumes
Add anonymous volume -v /app/node_modules to container to make sure the the node module folder doesn't get overwritten by our bind mounts folder content.
6
docker run -v /app/data ...
Creates annonymous volume that is attached to single container, survives container shutdown/restart, unless --rm flag is used. Cannot be shared across containers and be re-used. Volume can be added to the Dockerfile.
7
docker run -v data:/app/data ...
Creates named volume - cannot be added to the Dockerfile. Created in general - not tied to any specific container. Thanks to that it survices shutdown/restart - removal via Docker CLI. Can be shared accross containers and re-used for same container (across restarts).
8
docker run -v /path/to/code/:app/code ...
Creates Bind Mount. Location on host file system, not tied to any specific container. It survices container shutdown/restart - removal on host fs. Can be shared across containers and re-used for same container (across restarts).
9
docker run -d -p 3000:80 --rm --name feedback-app -v feedback:/app/feedback -v "/Users/andrew/training/data-volumes-01-starting-setup:/app:ro" -v /app/node_modules -v /app/temp feedback-node:volumes
Adding :ro at the end of container internal path - which overwrites default which is read-write to read-only. This ensures that docker will not be able to write into our folder or subfolders (only user on its host machine can do).
10
docker volume inspect [volume_name]
Check extra info about the volume, examle usage: docker volume inspect feedback

Example Dockerfile using Volumes

1
FROM node
2
​
3
WORKDIR /app
4
​
5
COPY package.json .
6
​
7
RUN npm install
8
​
9
COPY . .
10
​
11
EXPOSE 80
12
​
13
VOLUME [ "/app/feedback" ]
14
​
15
CMD [ "node", "server.js" ]
Copied!

ARG and ENV Variables

  • ARG - available inside of the Dockerfile, not accessible in CMD or any application code, example usage: docker build --build-arg
  • ENV - available inside of the Dockerfile and in application code, set via Dockerfile or --env or -eon docker run command, for example:
1
# using --env flag
2
docker run -d -p 3000:8000 --env PORT=8000 --rm --name feedback-app -v feedback:/app/feedback -v "/Users/andrew/training/data-volumes-01-starting-setup:/app:ro" -v /app/node_modules -v /app/temp feedback-node:env
3
​
4
# we can also have .env file inside our folder structure and run env with --env-file:
5
docker run -d -p 3000:8000 --env-file ./.env --rm --name feedback-app -v feedback:/app/feedback -v "/Users/andrew/training/data-volumes-01-starting-setup:/app:ro" -v /app/node_modules -v /app/temp feedback-node:env
6
​
Copied!

Example Dockerfile using ARG

1
FROM node
2
​
3
WORKDIR /app
4
​
5
COPY package.json .
6
​
7
RUN npm install
8
​
9
COPY . .
10
​
11
ARG DEFAULT_PORT=80
12
​
13
ENV PORT $DEFAULT_PORT
14
​
15
EXPOSE $PORT
16
​
17
# VOLUME [ "/app/node_modules" ]
18
​
19
CMD [ "npm", "start" ]
Copied!

Using ARG Command example

1
docker build -t feedback-node:dev --build-arg DEFAULT_PORT=8000 .
Copied!

Summary

  • Containers can read and write data, but written data is lost if the container is removed.
  • Volumes are folders on the host machine hdd, managed by Docker, which are mounted into the container (made available, mapped). Can help with data storage. Volumes persist if container shuts down/restarts. A container can weite data into a volume and read data from it. We have: - Named Volumes - survuve container removal and can be used to store persistent data - Anonymous Volumes are attached to a container - they can be used to save (temporary) data inside the container
  • Bind Mounts - folders on the host machine which are specified by the user and mounted into containers like Named Volumes (but we know the path on the host machine). Can help with direct container interaction.
  • Build ARGuments and Runtime ENVironment variables - can be used to make images and containers more dynamic/configurable