Author: Kevin Chhay
https://docs.docker.com/get-started/overview/
- Docker Desktop. Mac | Apple Silicon | Windows | Linux
- Docker for Visual Studio Code
https://docs.docker.com/desktop/windows/wsl/
Use docker help
or docker --help
to get an overview of available commands.
https://docs.docker.com/engine/reference/commandline/attach/
docker attach [OPTIONS] CONTAINER
https://docs.docker.com/engine/reference/commandline/build/
docker build [OPTIONS] PATH | URL | -
https://docs.docker.com/engine/reference/commandline/build/#options
Name, shorthand | Default | Description |
---|---|---|
--tag, -t | Name and optionally a tag in the 'name:tag' format |
# First navigate to directory with a Dockerfile
docker build .
docker build -t name:tagname .
https://docs.docker.com/engine/reference/commandline/cp/
docker cp [options] CONTAINER:SRC_PATH DEST_PATH|-
docker cp ./file.txt CONTAINER:/dir
docker cp CONTAINER:/var/logs/ /tmp/app_logs
https://docs.docker.com/engine/reference/commandline/image_prune/
docker image prune [OPTIONS]
https://docs.docker.com/engine/reference/commandline/image_prune/#options
Name, shorthand | Default | Description |
---|---|---|
--all, -a | Remove all unused images, not just dangling ones |
https://docs.docker.com/engine/reference/commandline/login/
docker login [OPTIONS] [SERVER]
docker login
https://docs.docker.com/engine/reference/commandline/ps/
docker ps [options]
https://docs.docker.com/engine/reference/commandline/ps/#options
Name, shorthand | Default | Description |
---|---|---|
--all, -a | Show all containers (default shows just running) |
docker ps -a
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
https://docs.docker.com/engine/reference/commandline/pull/
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
https://docs.docker.com/engine/reference/commandline/push/
docker push [OPTIONS] NAME[:TAG]
Make sure you're login first. See docker login.
Name your local images using one of these methods:
- When you build them, using
docker build -t <hub-user>/<repo-name>[:<tag>]
- By re-tagging an existing local image
docker tag <existing-image> <hub-user>/<repo-name>[:<tag>]
- By using
docker commit <existing-container> <hub-user>/<repo-name>[:<tag>]
to commit changes
Now you can push this repository to the registry designated by its name or tag.
docker push <hub-user>/<repo-name>:<tag>
docker push kevinc/hello-world:latest
https://docs.docker.com/engine/reference/commandline/rm/
docker rm [OPTIONS] CONTAINER [CONTAINER...]
https://docs.docker.com/engine/reference/commandline/rm/#options
Name, shorthand | Default | Description |
---|---|---|
--volumes, -v | Remove anonymous volumes associated with the container |
https://docs.docker.com/engine/reference/commandline/rmi/
docker rmi [OPTIONS] IMAGE [IMAGE...]
https://docs.docker.com/engine/reference/commandline/run/
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
https://docs.docker.com/engine/reference/commandline/run/#options
Name, shorthand | Default | Description |
---|---|---|
--detach, -d | Run container in background and print container ID | |
--interactive, -i | Keep STDIN open even if not attached | |
--name | Assign a name to the container | |
--publish list, -p | Publish a container's port(s) to the host | |
--rm | Automatically remove the container when it exits | |
--tty, -t | Allocate a pseudo-TTY |
# This binds port 80 of the container to TCP port 3000 of the host machine.
docker run -p 3000:80 image
# The -it instructs Docker to allocate a pseudo-TTY connected to the container’s stdin; creating an interactive bash shell in the container.
docker run -it image
https://docs.docker.com/engine/reference/commandline/start/
docker start [OPTIONS] CONTAINER [CONTAINER...]
https://docs.docker.com/engine/reference/commandline/start/#options
Name, shorthand | Default | Description |
---|---|---|
--attach, -a | Attach STDOUT/STDERR and forward signals | |
--interactive, -i | Attach container's STDIN |
docker start my_container
docker start -ai my_container
https://docs.docker.com/engine/reference/commandline/stop/
docker stop [OPTIONS] CONTAINER [CONTAINER...]
docker stop my_container
https://docs.docker.com/engine/reference/commandline/tag/
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
docker tag demo-app:latest kevinc/hello-world
https://docs.docker.com/engine/reference/builder/#from
The FROM
instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM
instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.
FROM [--platform=<platform>] <image> [AS <name>]
or
FROM [--platform=<platform>] <image>[:<tag>] [AS <name>]
or
FROM [--platform=<platform>] <image>[@<digest>] [AS <name>]
# Creates a layer from the lambda/nodejs:14 AWS Lambda base image for NodeJS
FROM public.ecr.aws/lambda/nodejs:14
https://docs.docker.com/engine/reference/builder/#workdir
The WORKDIR
instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR
doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.
WORKDIR /path/to/workdir
WORKDIR /app
https://docs.docker.com/engine/reference/builder/#copy
The COPY instruction copies new files or directories from and adds them to the filesystem of the container at the path .
Multiple resources may be specified but the paths of files and directories will be interpreted as relative to the source of the context of the build.
COPY [--chown=<user>:<group>] <src>... <dest>
or
COPY [--chown=<user>:<group>] ["<src>",... "<dest>"]
# Assumes there is a package.json file in the app directory.
# The AWS base images provide the following environment variables:
# LAMBDA_TASK_ROOT=/var/task
# LAMBDA_RUNTIME_DIR=/var/runtime
COPY . ${LAMBDA_TASK_ROOT}
https://docs.docker.com/engine/reference/builder/#run
The RUN
instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.
Layering RUN
instructions and generating commits conforms to the core concepts of Docker where commits are cheap and containers can be created from any point in an image’s history, much like source control.
RUN <command>
RUN npm install
https://docs.docker.com/engine/reference/builder/#expose
The EXPOSE
instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.
EXPOSE
instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.
EXPOSE <port> [<port>/<protocol>...]
# EXPOSE instruction does not actually publish the port. See note above.
EXPOSE 80
https://docs.docker.com/engine/reference/builder/#cmd
There can only be one CMD
instruction in a Dockerfile. If you list more than one CMD
then only the last CMD
will take effect.
The main purpose of a CMD
is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT
instruction as well.
CMD ["executable","param1","param2"]
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "index.handler" ]