Skip to content

A cheat sheet and a beginner friendly walk through docker and it myriad functionalities.

Notifications You must be signed in to change notification settings

alfahami/hands-on-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DOCKER CHEAT SHEET

In this cheat sheet, I'll be using apache image and php. At the end we'll build a multi-staged php mvc news app with Docker.

BASICS COMMANDS

  • Container's ID

The CONTAINER ID is the first 12 characters of the full container ID printed out after running docker container run.
The NAME is generated by docker and can be renamed by user later.

  • Create a container with a custom name and publish it to a port<host-port:container-port> without running.
docker create --pulblish | -p 8080:80 --name apache-server httpd 
8fad9d84692858682f33ff2b60d3ee43e946af96d062c1fb3aa399ea082e5345
  • Running a container (create and start)
docker container run -p 8080:80 --name apache-server httpd 

# or we don't have to precise "container"

docker run -p 8080:80 --name apache-server httpd
  • Naming and renaming a container
docker container run --detach --publish 8080:80 --name apache-server httpd

# Renaming

docker container rename <contaienr ID> <new name>
  • Running a container in the background
docker container run --detach | -d -p 80800:80 --name apache-server
  • Running a container interactively
docker container run -it | --interactive --tty
#
docker container run -it ubuntu 
  • Starting and restarting a container
docker container start apache-server | <container ID=output of previous command>
apache-server
  • Stopping a container (send a SIGTERM signal)
docker container stop apache-server # or container ID
  • Restarting a stopped container
docker container restart apache-server # or container ID
  • Killing a container (send a SIGKILL signal)
docker container kill apache-server # once killed it can't be restarted
  • Running and stopping (SIGTERM) a container after Ctrl + C
docker container run --rm -it ubuntu
  • Removing a stopped SIGTERM container
docker container rm | -f <container ID | name>
#
docker container rm apache-server
  • Removing all dangling (stopped SIGTERM) containers
docker container prune
  • Listing runnning containers
docker container ps | ls
  • Remove unused data
docker system prune

Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.

  • Listing running and stopped containers
docker container ps -a | --all | ls -a | ls --all
  • Executing command inside a container (image configured to receive arguments)
docker container run --rm alpine uname a
Linux 094a0cbf37ad 4.19.0-16-amd64 #1 SMP Debian 4.19.181-1 (2021-03-19) x86_64 Linux

In the command above the uname -a is passed through the alpine image and get executed.

  • Bind Mounts for executable images but not only

When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine.

docker run -d -it --name devtest --mount type=bind,source="$(pwd)"/target,target=/app nginx:latest

Using the --volume | -v flag.

docker run -d -it --name devtest -v "$(pwd)" target:/app nginx:latest

# this command will let us verify if the bind mount was created correctly

docker inspect devtest (container name)
  • Inspecting a Docker object

Return low-level information on Docker Objects. It provides detailed information on constructs controlled by Docker.

docker inspect [OPTIONS] NAME[ID] [NAME | ID ...]

IMANGE MANIPULATIONS

According to the officail docs

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.
  • Creating images
# Make sure we're in the same folder as the Dockerfile 

docker image buld -t <image-name> build . 

# <image-name> will be used to run the built image.
# . indicate the build to be run from the current directory where Dockerfile exists.
  • Our image should have httpd pre-installed

  • The image should start apache server upon running

  • Dockerfile

#base image 
FROM debian:latest 

RUN apt-get update 
RUN apt-get install apache2 -y 
RUN apt-get clean

COPY index.html /var/www/html/

EXPOSE 80

# running apache in foreground
CMD ["apache2ctl", "-D", "FOREGROUND"] 
  • Tagging an image (assigning a custom identifier to our image)
docker image build -t (--tag) <image-repository>:<image tag name> <path>

# example

docker image build --tag custom-apache:packaged httpd:packaged .
  • Changing the tag of an already tagged built image
docker image tag <image id> <image repository>:<image tag>

## or ##

docker image tag <image repository>:<image tag> <new image repository>:<new image tag
  • Listing and removing docker images
# listing all available images
docker image ls

# removing one given image
docker image rm <image identifier or image name>

#removing all existing images
docker image prune | --force (-f) | --all (-a)
  • Show the history of an image
docker image history [OPTIONS: --human (-H) | --format | --no-trunc | --quiet (-q)] image