Janne Alatalo
janne.alatalo@jamk.fi
docker container run
to create a container from some image
# Starts a container with nginx-container name
# from the nginx dockerhub image
docker container run --name nginx-container -p 80:80 -d nginx
--name nginx-container
the container name
-p 80:80
forwards the local port 80
to container port 80
-d
runs the container in the background
nginx
the last parameter is the image name
-p
or --publish
argument for the docker container run
commandhostPort:containerPort
ip:hostPort:containerPort
containerPort
ip::containerPort
# Bind to host port 8080 and forward it to container port 80
docker container run --name nginx-8080 -p 8080:80 -d nginx
# Bind to host port 127.0.0.1:8888 and forward it to
# container port 80
# (only traffic with dest ip 127.0.0.1 is forwarded)
docker container run \
--name nginx-localhost -p 127.0.0.1:8888:80 -d nginx
docker container ls
docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
249cf4240596 nginx "nginx -g 'daemon of…" 18 seconds ago Up 16 seconds 127.0.0.1:8888->80/tcp nginx-localhost
f549ece8fc1e nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:8080->80/tcp nginx-8080
f18843473186 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp nginx-container
docker container run \
-d \
--name postgres \
-e POSTGRES_USER=user1 \
-e POSTGRES_PASSWORD=supersecret \
postgres
List the images: docker image ls
docker image ls
Download an image: docker image pull
docker image pull nginx
Remove an image: docker image rm
docker image rm nginx
New command | Short command |
---|---|
docker container {run, start, stop, exec, logs, cp} | docker {run, start, stop, exec, logs, cp} |
docker container ls | docker ps |
docker image {pull, push, build, tag} | docker {pull, push, build, tag} |
docker image ls | docker images |
docker image rm | docker rmi |
mkdir /tmp/www
docker container run \
--name nginx-container \
-p 80:80 \
-v /tmp/www:/usr/share/nginx/html:ro \
-d \
nginx
-v /tmp/www:/usr/share/nginx/html:ro
source directory (host):
destination directory (container)[:optional param]
echo '<html><body><h1>Hello world!</h1></body></html>' > /tmp/www/index.html
# Create a volume named psql-vol
docker volume create psql-vol
# Same syntax as the bind mount
docker container run \
--name postgresql-container \
-p 127.0.0.1:5432:5432 \
-v psql-vol:/var/lib/postgresql/data:rw \
-d \
postgres
# List the volumes
docker volume ls
# Show the volume information
docker volume inspect psql-vol
docker container stop postgresql-container
docker container rm postgresql-container
docker volume rm psql-vol
docker exec -it postgresql-kontti bash
-it
is required for interactive commands
docker exec postgresql-kontti cat /etc/passwd
# Copy file from the container to the host
docker container cp nginx-kontti:/etc/nginx/nginx.conf /tmp/nginx.conf
# Copy file from host to the container
docker container cp /tmp/nginx.conf nginx-kontti:/etc/nginx/nginx.conf
# Print all of the postgresql-container container logs
docker container logs postgresql-container
# Real time follow mode
docker container logs -f postgresql-container
ip addr
and see docker0
interfacedocker inspect CONTAINER_NAME
command
# Create a new network
docker network create my-network
# Start two containers
docker container run -d -t --rm --name container1 --net my-network busybox
docker container run -it --rm --net my-network busybox
# Inside the container
ping container1
php:7.1-apache
docker image from Dockerhub/tmp/php
directoryphp:7.1-apache
image/var/www/html
directorydocker run
or docker container run
creates a new container-p
argument to forward a port from host to containermysql:5.7
(Wordpress requires this version)/var/lib/mysql
directory outside of the mysql containerWORDPRESS_DB_HOST
WORDPRESS_DB_USER
WORDPRESS_DB_PASSWORD
WORDPRESS_DB_NAME
MYSQL_ROOT_PASSWORD
MYSQL_DATABASE
MYSQL_USER
MYSQL_PASSWORD
# Docker base image
FROM debian:buster-slim
# Commands that are executed during image creation
RUN apt-get update \
&& apt-get install -y nginx
RUN echo "hello world" > /var/www/html/index.html
EXPOSE 80
VOLUME /var/www/html
# Command that is executed when the container is started
CMD ["nginx", "-g", "daemon off;"]
FROM
Defines the base imageRUN
Executes a command during the building phaseCOPY
and ADD
Copies files inside imageENTRYPOINT
ja CMD
Defines the command that is executed at container startupEXPOSE
Information about the ports that the services are usingVOLUME
Defines a mount pointUSER
Defines the user that is used to execute the following RUN
, ENTRYPOINT
and CMD
instructions
mkdir /tmp/dockerfile-example
cd /tmp/dockerfile-example
curl http://alaja.pages.labranet.jamk.fi/docker-koulutus/files/example-dockerfile > Dockerfile
docker build -t my-nginx-image .
# Images can be tagged with version too
docker build -t my-nginx-image:v0.1 .
docker run --name my-nginx-container -d my-nginx-image:v0.1
npm run-script build
builds an optimized build for the projectbuild
directory can then be served with any normal http serverREACT_APP_API_ROOT
during build time with ARG
and --build-arg
argumentsudo usermod -a -G docker ${USER}
# Ubuntu instructions below.
# For generic linux instructions follow this guide:
# https://cloud.google.com/sdk/docs/quickstart#linux
sudo apt-get install apt-transport-https ca-certificates gnupg
echo "deb https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install google-cloud-sdk
# Initialize the tool
gcloud init
# Authenticate docker daemon to registry
gcloud auth configure-docker
docker image tag my-nginx-image:v0.1 eu.gcr.io/yourprojectid/my-nginx-image:v0.1
docker image push eu.gcr.io/yourprojectid/my-nginx-image:v0.1
docker-compose build
should build the imagesdocker-compose up
should start the containersREACT_APP_API_ROOT