Using docker as develop environment
I’ll get straight to the point.
For this tutorial I'm going to use fedora linux image and will install nodejs, using docker for OSX.
I'm assuming you already have docker installed, if not you can do it right now.
Download Docker Community Edition, and follow the instructions for it here.
Before we continue, let me answer some questions you probably have about Docker:
- 1- what is docker?
Docker is a software container platform. - 2- what is container?
Unlike VMs, containers do not bundle a full operating system, only the libraries and settings required to make the software work are needed. - 3- why should I use docker?
By using docker you can eliminate old phrases like these from your vocabulary: “Works on my machine”, “Ouch! There is maybe a configuration missing”, “Someone has changed my code in the production machine. I swear!” (lol), you can also eliminate problems with collaborating on code. You can also use docker to run apps side-by-side in different containers (isolated) taking advantage of compute density. - 4- how a container will help me?
The contained system can help you to guarantee that your software will have the same behavior regardless of where it is deployed.
OK, Let's begin!
For this example I'll use react through create-react-app and the purpose of this mini tutorial is to show you how to configure docker to use as a dev environment, not how to use react.
Step 1:
In your project folder you run create-react-app docker-test, it will take a few minutes.
Step 2:
Go to the docker-test folder
Step 3:
Create two files, "Dockerfile" and "docker-compose.yml" (Compose is a tool for defining and running multi-container Docker applications — for example if you have different containers for different kinds of services).
Step 4:
Open both files in your favorite editor.
Step 5:
Copy the code below and paste inside the Dockerfile
Let's go through this code line by line.
First of all, it is creating a personal image from a public and official image of fedora linux, you can use any other that you are more familiar with, for example the nodejs image (if you choose this option, maybe you don't need line 5 of that code).
The FROM fedora
command says that this image will run on top of fedora linux's latest version.
Next, there are several 3 RUN
commands which will execute code in this image.
The first RUN
command uses yum update -y
, the fedora package manager (yum)
will update all packages. The second RUN
command will install the nodejs, the third one will create a /src
folder and open it.
The WORKDIR
command sets /src
as the working directory.
So Simple!
Step 6:
Copy the code below and paste inside the docker-compose.yml
Again, let’s go through this code line by line.
Line 1: The version
command says the version of docker-compose file format we are going to use.
Line 3: service
section.
Line 4: we have the service called abdalla
(could be anything you want);
Line 5: we are telling to docker compose how to build our container, in this case it will use our Dockerfile.
Line 6: we have the container name
(could be anything you want).
Line 7: we have the image name
this specifies which image should be used for this particular service container.
Line 8–9: we have the volumes
you can have as many as you want, but in our case we are specifying only one. The "." is your current directory that will be mounted into the container in the folder "/src".
Line 10: we have tty
it will help us to have an interactive mode.
Line 11–12: we have ports
that are used to set the ports that the application will listen to, the first argument is in your local environment and the second one is the container port. (for example, you application will be running inside the container through port 3000 but to access you want to run through port 5000 you set this configuration "5000:3000");
Line 13: we have the command
that will run as soon as your container is up.
Step 7:
run the command bellow (only one time)
Step 8:
Every time you want to run your container and your app, just run the command bellow
Bonus
source code: https://github.com/abdalla/docker-test
code video: