Docker Series — Starting your first container
Now that we have a simple understanding of what Docker does, let’s dive deeper! To get started install docker-ce by following the instructions on the page below.
https://www.docker.com/community-edition
Once Docker is installed you can easily start a Docker container on your own. If at any point you need more information about a command or want to look deeper into something, jump down to the bottom of the page to see the my Docker Cheat Sheet which includes a summary of commands and links to the official Docker documentation.
Run
Next open a terminal for OSX or command line in windows and run
> docker run -d -p 80:80 --name pintail-whoami pintailai/pintail-whoami:0.0.1
Open a browser and go to localhost and you should see the Pintail.ai Docker Example
That’s all it takes to run a container!
Remember docker is spinning up Virtual Machines within your host operating system. Docker spins up a separate and isolated Virtual Machine container and acts as the middleman between your host operating system and the container.
Let’s break down all the arguments of the run command below so we understand what docker is really doing to start and manage this container.
> docker run -d -p 80:80 --name pintail-whoami pintailai/pintail-whoami:0.0.1
docker — Specify the docker program
run — Specify the run command to tell Docker you want to run a new container
-d — Tells docker to run the new container in detached mode. This will force the container to run in the background rather than locking up your terminal window
-p — tells Docker what ports to map from the container to your host OS. The format is {host-os-port:container-port}. In this case docker will map port 80 from your host OS to port 80 inside the container
- - name — names the container “pintail-whoami”
pintail/pintail-whoami:0.0.1 — this last argument indicates the specific image you would like to start. For images hosted on Docker Hub the format is {organization_name}/{image_name}:{version}
Remember to keep track of the containers and images you have pulled or running on your machine. It can be very easy to lose track of what container or what version you are working with. So be sure to stop your containers when you are done to prevent unused containers running in the background. Also if you know you aren’t going to use an image any more remove it from your machine so it doesn’t take up disk space. When you are done running the pintail-whoami image, run the commands below to stop the container and then remove the image.
View Running Containers
docker ps
Stop Container
docker stop pintail-whoami
Remove Container
docker rm pintail-whoami
View Images
docker images
Remove Image
docker rmi pintailai/pintail-whoami:0.0.1
Lots of organizations have official Docker images published on Docker Hub. There you can find images from NodeJS to Microsoft SqlServer. Most of which you can run locally using only one Docker command. To look at published Docker images go to Docker Hub.
Running pre built images works great if you can find exactly what you want but what if you want to customize something or create your own custom docker image. Tune in next week for my walk through of how to write your first docker image.
Cheat Sheet
Run Container
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG…]
Use docker run to start new containers
https://docs.docker.com/engine/reference/run/
Stop Container
docker stop [OPTIONS] CONTAINER [CONTAINER…]
Use docker stop to stop one or more running containers.
https://docs.docker.com/engine/reference/commandline/stop/
List Containers
docker ps [OPTIONS] [-a]
Use docker ps to list running containers on your machine. Using the -a option will show all containers in any state not just running. Don’t forget when you stop a container it doesn’t always get automatically removed.
https://docs.docker.com/engine/reference/commandline/ps/
Remove Container
docker rm [OPTIONS] CONTAINER [CONTAINER…]
Use docker rm to remove a stopped Docker container. Be careful! Removing a stopped Docker container can cause any data stored within the container to be lost. Do not remove containers unless you are sure the any contained data is either backed up or unnecessary
https://docs.docker.com/engine/reference/commandline/rm/
List Images
docker images [OPTIONS] [REPOSITORY[:TAG]]
Use docker images to list all docker locally downloaded Docker images
https://docs.docker.com/engine/reference/commandline/images/
Remove Image
docker rmi [OPTIONS] IMAGE [IMAGE...]
Use docker rmi remove one or more docker images from your machine
Docker Series — What is Docker?
Docker Series — Creating your first Dockerfile
Docker Series — Building your first Docker image
Docker Series — Moving past one container with Docker Compose
Docker Series — Cheat Sheet
Coming soon!
Docker Series — Moving past one machine with Docker Swarm and Kubernetes