Docker Toolbox Environment


Docker is an container based platform tool, this is allows developers to run applications in a copy of the live environment. Docker uses an image to create a container to run the application in. A container is not persistent, any changes made within them are gone when the container is closed, to save our changes a container we can use a volume.

Getting Docker installed

Since I have Windows 10 Home I am using their legacy solution Docker toolbox if you have Mac or Windows 10 pro or higher you can use Docker Desktop.

To get started, first download and install Docker Toolbox.

The Toolbox installer will install VirtualBox, Docker Engine, Docker Machine, Docker Compose and Kitematic.

After the installer has run there will be two shortcuts on your desktop: Kitematic and Docker Quickstart Terminal.

Open Docker Quickstart Terminal. It will start a VirtualBox VM running Docker Engine, then configure the command-line environment so that you can talk to it.

Docker commands

To make sure docker is installed correctly run in the terminal:

docker run hello-world

Since we don’t have the hello-world image on our system docker will go and pull it down to our local system then run it. After it has downloaded and run you should see “Hello from Docker!” followed by a further description of what has just happened.

To see the docker container running you started type the following command:

docker ps -a

This will give you a list of your current docker containers and information on their status.

To give your docker container a unique name you can pass the --name parameter:

docker run --name my-hello-world hello-world

Now if you run docker -ps a you will see two containers. The one we ran originally and the one with your new name.

To see all the images on docker run the following command:

docker images

To remove a container from docker run the following command:

docker rm my-hello-world

Bonus to remove all containers from docker you can run the following:

docker rm $(docker ps -a -f status=exited -q)

You can always refer to run --help to get all the details for the syntax.

Ubuntu Container

Now let’s take what we have learned and start up a ubuntu container. This image is available on https://hub.docker.com/ this will go an pull down the docker ubuntu image.

docker run -it --name my-linux-container ubuntu bash

After the image is downloaded and running using the bash at the end of the command automatically puts you in the container to run commands.

You can exit the container by typing:

exit

Volumes

Let’s make a container that actually refers to some local files. This can be done with the -v flag. The notation would be

-v [local/path]:[container/path]

The following code is written for my machine:

docker run -it --name my-ubuntu-container --rm -v /c/Users/Documents/code:/my-code ubuntu bash

Then you can execute the following line (since you are on the environment) to see the files:

ls my-code/

What we have done is taken all the content within the directory /c/Users/Documents/code and loaded it into the directory /my-code of the docker container.

Be careful! Your container now has actual files from your hard drive. So if you remove a file from the container it will be removed on your system as well without asking permission. It is a much better idea to only link the sub folder that you intend to use in your application.

Finding your Docker IP

To access your docker toolbox environment you will need the ip address, you can find the address by running this command in the terminal.

docker-machine ip default

Docker Compose

I have created a docker compose example at https://github.com/mattyace/lemp-docker

Pull the repo to your machine then navigate into the file using the command line then run the following command:

docker-compose up -d

This will show the following code.

$ docker-compose up -d
Creating network "lemp-docker_lemp-network" with driver "bridge"
Creating Matts_LEMP_php     ... done                                                                                                                                       Creating Matts_LEMP_mariadb ... done                                                                                                                                       Creating Matts_LEMP_nginx   ... done 

If you navigate to you localhost (or if you are using docker toolbox the ip address of the toolbox) you should now see a “Welcome to Nginx” page. If you navigate to /index.php you will see a “Welcome fo Matty’s LEMP Docker Environment!” with php details.

If you are feeling adventurous you can access the database of the mysql instance with username root and password masterpass on port 3306.

You can close these new containers with docker-compose down

Feel free to have a play with the setup of the containers and how the docker-compose file is set up. If you want to change details of the containers you can edit the .env file.