Getting started with Docker (Part I)

Fatma Ali
3 min readJun 27, 2018

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

Unlike creating a virtual OS like VMs, docker allows applications to use the same Linux kernel as the host machine. This makes docker more light-weight and perfomant than VM’s. It is an open source tool made to make the lives of developers and system admins a lot easier by vanquishing the “works on my machine” problem.

Getting Started

For this tutorial, we are going to learn how to set up and basic commands I use almost everyday when working with containers.

Step 1: Install Docker

In this step, you could either install docker, or head over to Docker playground (make sure you have a docker hub account) and get started playing with docker right away!

Step 2: Basic Commands

You can confirm if docker is installed by running the command below and it should return the name of the current version of docker you’re running:

$ docker --versionDocker version 18.03.1-ce, build 9ee9f40

The next step is to fetch a mysql image from docker hub. A container is launched by running an image. An image is an executable package that includes everything needed to run an application — the code, a runtime, libraries, environment variables, and configuration files.

To pull the mysql image, we will run the command below:

$ docker pull mysqlUsing default tag: latest
latest: Pulling from library/mysql
07a152489297: Pull complete
Digest: sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
Status: Downloaded newer image for mysql:latest

To see a list of the images now available in your system:

$ docker image lsREPOSITORY          TAG                 IMAGE ID            SIZE
mysql latest 8d99edb9fd40 445MB

To run the mysql image, we will run:

$ docker run --name mysql_container -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql

The above command runs the docker image of mysql. The --name tag names the container mysql_container, if we had not provided a name, docker would have given a random name to our container. The -e tag sets the environment variable which in this case is the mysql root password to my-secret-pw.

The -d tag tells docker to run this container in the background as a daemon, thus closing the shell won’t stop the container, (the opposite of this is -it), and finally we call the image name which is mysql.

To see the list of all running containers:

$ docker ps

To log into a running container:

$ sudo docker exec -ti mysql_container bash

once inside the container, you can execute commands such as ls ,cd, mkdir because it is a based on the unix kernel. Once in the container, you could try connecting to your mysql instance using mysql -uroot -pmy-secret-pw. To exit the container just hit exit .

To stop a running container simply hit:

$ docker stop mysql_container

When you run sudo docker ps, you won’t see mysql_container in the list of running containers, to see a list of all running and stopped containers:

$ sudo docker ps -a

The -a tag means all containers. To restart the container

$ sudo docker start mysql_container

To remove a container means you cannot restart it again. You simply do that with this command (note that you cannot remove a running container):

$ sudo docker rm mysql_container

And that concludes the basic docker commands that will get you started with docker. To see all the options available for a particular command, for example start, you can run:

$ docker start --help

If you have reached this far, congrats! :-D In the next tutorial, we are going to look at docker volumes!

--

--

Fatma Ali

Software Engineer, passionate about code, food & art. Bollywood dancer sometimes💃 Find me on GitHub https://github.com/fatmali