Tries to make it really easy to run your code on any machine, local or on the cloud.
Docker allows you to containerize running processes, running the code in a configured, self-contained “container” that has all the necessary dependencies installed.
There are docker images, which are files that have dependencies and configuration to run a program.
We can run or spin up an image, and this image will be a running instance in a container.
Common commands.
docker run [-it] <image> [custom-entry-command]
Tells Docker to spin up an instance of image, with an optional entry command.
If we use -it with a custom command of sh, we can open a new container straight into the shell.
Docker will fetch the image from DockerHub if this is the first time running this image.
This is really just a combination of docker create and docker start.
docker start [-a] <instance-id>
Starts an instance that was previously created.
Can be used to run a container that was previously shut down.
-a flag forwards output from the instance to the currently open terminal instance.
“attach” your terminal to the Docker instance.
docker ps [--all]
Shows all running Docker instances.
--all flag shows all Docker instances, including ones that have been terminated already.
docker exec [-it] <container-id> <command>
Used to execute a command inside a running container.
-it lets you type input directly into the container.
-i redirects your input to STDIN of the container.
-t makes the output actually nice and useable.
Can set the <command> as sh to just work in the container as normal.
docker logs <container-id>
Output the past logs of the container into your terminal.
stop sends a SIGTERM signal, allows for time to cleanup.
Prefer this method!
kill sends a SIGKILL signal, no time for cleanup.
docker rmi <image-name>
manually remove an image or tag.
docker system prune
Clear all old Docker containers.
Creating images.
We will create a Dockerfile.
Through some magic, the Docker client and server will create a useable image.
Example redis-server image.
# Use an existing docker image as a baseFROM alpine# Download and install a dependencyRUN apk add --update redis# Tell the image what to do when it startsCMD ["redis-server"]