We can deploy/run containers with various tools. We see what are the options available to run the containers
- Running container from local/remote image, Image can be pulled from three locations (Docker Hub / Docker Trusted Registry) / Private Registry)
- Running container by building an image from a existing running container
- Running container by building an image from a Dockerfile
- Running container by building an image from a Docker Compose
- Running container by building an image from a Docker Stack Deploy (Swarm Cluster)
A Dockerfile is a set of instructions which are used to construct a Docker image. These instructions are called directives
- FROM: Starts a new build stage and sets the base image. Usually must be the first directive in the Dockerfile (expect ARG can be placed before from)
- ENV: Set environment variables. These can be referenced in the Dockerfile itself and are visible to the container at runtime
- RUN: Creates a new layer on top of the previous layer by running a command inside that new layer and committing the changes
- CMD: Specify a default command used to run a container at execution time.
- EXPOSE: Documents which ports are intended to publish when running a container
- WORKDIR: Sets the current working directory for subsequent directives such as ADD, COPY, CMD, ENTRYPOINT, etc. Can be used multiple times to change directives throughout the Dockerfile. You can also use a relative path, which sets the new working directory relative to the precious working directory
- COPY: Copy files from the local machine to the image
- ADD: Similar to COPY, but can also pull files using a URL and extract an archive into loose files in the image.
- STOPSIGNAL: Specify the signal that will be used to stop the container
- HEALTHCHECK: Specify a command to run in order to program a custom health check to verify that the container is working properly
Example Docker file to run a tomcat container: (Dockerfile)
https://github.com/cloudwwh/dockerfile/blob/master/wordpress
FROM centos
MAINTAINER linuxraja@gmail.com
RUN mkdir /usr/share/tomcat/
WORKDIR /usr/share/tomcat/
RUN curl -O https://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.51/bin/apache-tomcat-8.5.51.tar.gz
RUN tar xvfz apache-tomcat-8.5.51.tar.gz
RUN mv /usr/share/tomcat/apache-tomcat-8.5.51/* /usr/share/tomcat/
RUN rm -rf /usr/share/tomcat/apache-tomcat-8.5.51
RUN yum -y install java
RUN java -version
WORKDIR /usr/share/tomcat/webapps
COPY sample.war .
EXPOSE 8080
CMD [“/usr/share/tomcat/bin/catalina.sh”, “run”]
Docker is a powerful tool for running containers, but many applications consist of a collection of multiple containers running different software and interacting with one another. These complex applications can become difficult to maintain when all of their components are managed separately. Docker Compose offers a solution to this problem by allowing you to declaratively describe a set of multiple resources and manage them as a unit. Docker Compose is a tool that allows you to run multi-container applications defined using a declarative format. DCA primarily covers Docker Compose as it relates to stacks in Docker Swarm. Docker compose uses YAML files to declaratively define a set of containers and other resources that will be created as part of the larger application.
Setting up a new Docker compose project: Make a directory to contain your Docker Compose Project
- Change to the project directory
- Add a docker-compose.yml file to the directory
- Define your application in docker-compose.yml
Example: https://github.com/cloudwwh/docker-compose/blob/master/docker-compose.yaml
version: ‘3.3’
services:
db:
image: mysql:5.7
volumes:
– db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: password
wordpress:
depends_on:
– db
image: wordpress:latest
ports:
– “8000:80”
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
Docker stack is a command that’s embedded into the Docker CLI. It lets you manage a cluster of Docker containers through Docker Swarm. It just so happens both Docker Compose and the docker stack command support the same docker-compose. yml file with slightly different features.
https://github.com/cloudwwh/docker-stack/blob/master/docker-compose.yaml
openssl rand -base64 20 > db_password.txt
openssl rand -base64 20 > db_root_password.txt
version: ‘3.1’
services:
db:
image: mysql:5.7
volumes:
– db_data:/var/lib/mysql
networks:
mysql_internal:
aliases: [“db”]
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD_FILE: /run/secrets/db_password
secrets:
– db_root_password
– db_password
wordpress:
depends_on:
– db
image: wordpress:latest
networks:
mysql_internal:
aliases: [“wordpress”]
wordpress_public:
ports:
– “8001:80”
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD_FILE: /run/secrets/db_password
secrets:
– db_password
secrets:
db_password:
file: db_password.txt
db_root_password:
file: db_root_password.txt
volumes:
db_data:
networks:
mysql_internal:
driver: “overlay”
internal: true
wordpress_public:
driver: “overlay”
Docker Commands:
- Simple docker run command: docker run -d -p 80:80 –name host_nginx nginx
- Docker build image from Dockerfile: docker image build -t IMAGE_NAME (Dockerfile should be in the CWD)
- Docker build image from Docker compose: docker-compose up -d (docker-compose.yml should be in the CWD)
- Docker run a container with Docker stack: docker stack deploy –compose-file docker-compose.yml STACK_NAME