DEV Community

Jimmy Kane
Jimmy Kane

Posted on

Making your applications run consistently with Docker

We all have been there. You have code that runs locally on your machine just fine so you push it to version control, someone else comes along and runs your code and it doesn’t work. It is a very frustrating position to be in, especially as a rather new programmer like myself. This problem though isn’t always this small, sometimes code that works fine on our machines and in the development server crashes in production.

To aid this problem, there is a way to package or contain your code as well as running it in a common form so it preforms the same way, no matter where it is! This idea is called containerization and it provides a consistent environement for code and projects to run in. One of the most popular companies in this space is Docker.

Getting Started with Docker Playground & Tutorial

If your familar with A Tour of GO Docker has a similar learning playground called Docker Playground. Here you can play around with a command line and run Docker commands and containers for FREE. This does though require an account though I just signed in using my GitHub account. For this tutorial we are going to be getting the Docker 101 Tutorial running so you have access to the documentation to begin your learning.

Step 1: Create a new Instance

Select the + ADD NEW INSTANCE button to create a new session/instance of the Docker terminal.
Image description
Image description

Step 2: Run the Docker Tutorial container

As it mentions on the labs page, click in the black terminal area next to the $ and paste in the following:
docker run -dp 80:80 docker/getting-started:pwd
Image description

Step 3: Open the tutorial

You may now notice a blue 80 next to the Open Port button, click this and you should have a new page open containing the Docker tutorial/
Image description

Understanding DockerFiles

To create a container for a project, there needs to be a set of instructions to tell Docker how to compile and run the project. Some services like pipelines use .yaml files to do this, Docker has its own method called DOCKERFILES. An example can be found here and we’ll disect what the red words mean now.

FROM

This is always the first command in a DockerFile and it declares what the base image of the container is. Think of this like the OS your code needs to run in, for our example our code is a Node app, thus it runs node:12. If you want to learn more, here is an article for your viewing pleasure.

WORKDIR

This establishes where your base working directory is inside of the container. If you open up terminal or command prompt now, the base working directory is your user folder. Image inside of your container, you are establishing where the terminal starts out.

COPY

This works exactly like the cp command in linux/unix. The first parameter is file to be moved and the second parameter is where you are moving it to. This example has two:

The first copies the package.json file to /home/node/app/package.json
The second command uses the . which represents all files and folders in a directory. In this line, it copies everything from the repo to the container at //home/node/app/..

RUN

This allows you to run a command like you would in the terminal but inside of the container. So this container will run yarn install to install all dependies from package.json. This triggers while the docker image is being built.

CMD

This takes in array of params and when the container is run, this command is run inside of the container. This example runs yarn start which starts the Node server. This command differs from RUN in the sense that this only runs when some runs the container, NOT while it is being built.

Top comments (0)