About
This post is a cheat sheet for Docker. I wrote this for myself so if I ever have trouble remembering some of the more common commands I can just look them up on my own site. And maybe someone out there on the internet will find this useful for themselves as well.
Note: After learning docker you might also want to learn about container orchestration tools like Kubernetes that will manage deployment and load balancing for you.
Installing Docker
Dockerfile
dockerfile
#Select base image to start from. (base image):(version) FROM ubuntu:latest #Execute command in the commad line inside the image. RUN apt-get -y update RUN apt-get install -y apache2 #Note that a service will be running on port 80. EXPOSE 80 #WORKDIR sets the current directory kind of like cd. WORKDIR /var/www/html #Copies file into the specified directory inside the container. COPY ./src/index.html /var/www/html/index.html #Specifies what should be run when the container starts up. ENTRYPOINT ["/usr/sbin/apache2ctl"] #CMD adds a command to the CLI when the docker image is started up. CMD ["-D", "FOREGROUND" ] #Example: docker run "yourImageName" -D "FOREGROUND"
Docker CLI Commands
//Build docker container. //docker, command, tag, local docker dir docker build -t test-build \. //Run the container. //docker, command, -it(interactive terminal), port external map to internal, image name docker run -it -p 80:8080 test-build //Run the container in the background. //docker, command, -d(detached), port external map to internal, image name docker run -d -p 80:8080 test-build //Show running docker containers. docker ps //Stop container. //docker, command, container name docker stop TestContainer //Show image builds. //docker, command docker images //Get bash access to container. //docker, command, it(interactive terminal), container ID, path to bash docker run -it 5sd4f564s6df /bin/bash //Or if the above doesn’t work: docker run -it 5sd4f564s6df /bin/sh //Removing images and containers. docker rm //remove container docker rmi //remove image //Prompts you for your credentials. docker login //Pulls an image from docker hub or a private repository. docker pull nameOfTheImage //Pushes an image to docker hub or a private repository. docker push nameOfTheImage
Docker Compose
Docker compose can be used to run multiple containers at once. For example, if you want to set up something like a WordPress site you will need a web server and a MySQL database. We can use the docker-compose.yaml file to define all these services and their behavior.
Note: Ideally you would just get a WordPress container image that has all of this already installed. This example is used just to demonstrate how to use docker compose.
docker-compose.yaml
#Specify the docker version we are using.
version: "3"
#Define services/containers.
services:
#1. - Web server.
web:
#Specify base image. Could be your image or one from docker hub.
image: "php:7.4-apache"
#Connects ./src from project folder to /var/www/html folder within the container.
volumes:
- "./src/wordpress:/var/www/html/wordpress"
#Restart on crash.
restart: 'always'
#Specifies that the DB should be running before the web server.
depends_on:
- db
#Maps internal docker ports to outer port.
ports:
- '8080:80'
#Specifies connection with the DB service.
links:
- db
networks: #Connects this container to myNewNetwork network.
- myNewNetwork
#2. - Database.
db:
#Specify base image.
image: mysql:latest
#Restart on crash.
restart: 'always'
#When a container is shut down it will lose all it's data.
#We can define volumes a wher we can store data Persistance
volumes:
- ./src/dbData:/var/lib/mysql
#Define environmental variables that will be passed to the container when it runs.
environment: #${ someVariableName } can be used to define a variable you will need to pass with the docker-compose command.
#This way whoever uses this can easily set their own user./pass. without changing the docker-compose file.
MYSQL_DATABASE: "wordpress"
MYSQL_USER: "user" #${USERNAME} <== example of how it can be used.
MYSQL_PASSWORD: "somepassword" #"${MYSQL_PASSWORD}" <== example how it can be used.
MYSQL_ROOT_PASSWORD: "somepassword" #"${MYSQL_PASSWORD}" <== example how it can be used.
networks: #Connects this container to myNewNetwork network.
- myNewNetwork
#Here we can define our networks.
networks:
myNewNetwork: #Define a new network.
driver: bridge #Sets bridge mode(similar to when you setup VMs). Docker Compose Commands
#Builds and runs all the containers/images specified in the docker-compose.yaml file. docker-compose up #Stops all the containers/images specified in the docker-compose.yaml file. docker-compose down





