Docker Engine: interface for running containers/creating
Docker Registry: where images are stored
Docker Hub: Cloud service for distributing containers
Installation
First, apt install curl
Use this command:
curl -sSL https://get.docker.com/ | sh
Run as normal user:
sudo usermod -aG docker joe
Reboot, see if docker commands work as normal user.
First Examples
docker run hello-world
docker - indicates to os that we are using this program
run - creates and runs a docker container
hello-world - which image to put in container
Container vs vm
A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.
By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.
Shows images that are installed or available locally to run
docker --help
What comands are available to me
docker logs
Shows any container output
Docker Examples
docker run debian echo "Hello world"
Docker has provisioned and launched our container, executed our echo command, and then shut down the container again. If you were to try to do something similar with a traditional VM, you would be waiting several seconds, possibly minutes.
Containers only run as long as their main process
Container isolation
-
Docker Examples
docker run -h CONTAINER -i -t debian /bin/bash
Gives container a hostname
docker ps
docker inspect gigantic_leavitt or whatever the name is
docker diff gigantic_leavitt #what changes have been made
docker logs gigantic_leavitt #cmd history
Docker Examples
docker ps -a or docker container ls -a
Shows all containers (including stopped ones)
docker rm gigantic_leavitt
Dockerfile
The dockerfile provides instructions as to how to build and run an image. It can map ports to outside world. Copy stuff into container (your application code). Set environment variables, and more.
Dockerfile Example
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Kind of like a cd. Changes current working directory to that which is specified.
Dockerfile (Volume)
The VOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers. A LOT of information about volumes can be found here.
VOLUME <mountpoint>
Dockerfile (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. To actually publish the port when running the container, use the -p or -P flag on docker run.
Dockerfile (Expose)
EXPOSE <port> [<port>/<protocol>...]
EXPOSE 8080
This will expose 8080 tcp port.
Dockerfile (RUN)
RUN <command> (or)
RUN ["executable", "param1", "param2"]
The first command is a shell command always executed at build.
The second does not automatically invoke shell /bin/sh -c.
Syntactically, you can add more than one CMD instruction in Dockerfile. However, the build system would ignore all the CMD instructions except for the last one. In other words, in the case of multiple CMD instructions, only the last CMD instruction would be effective.
Docker Networking
We also know that at container runtime, in order to actually attach to the exposed port created during the build process, we must pass in command line arguments like so:
docker run -d -p 80:80 apachephp
Other random docker things
Force remove running container:
docker container rm -f <containername>
Attach to running container and execute shell in interactive mode.
docker exec -it apachetest bash
docker attach may also work
To leave do a cntrl+p and cntrl+q.
Host network
The container will share the same network as the host OS.
curl localhost:80 #shouldn't get any result
docker run --rm -d --network host --name my_nginx nginx
curl localhost:80 #should give you a default nginx page
Docker Compose
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.