This is a super basic docker tutorial for absolute beginners.
To start, you definitely need to install docker on your operating system as the first thing. This is quite straightforward. Just visit docker's site and follow the instructions.
Create A NodeJS Application
Let's create a simple NodeJS app with the following
We'll need a
package.json file as well, with express installed. Run the following commands to achieve that:
Now add the
start script to
How do we dockerize this app?
Dockerize The Application
We'll start by creating a
Dockerfile in the root directory of our project folder.
Here's what it says:
- Create an image from a base image called
- The working directory inside the container would be
package*.jsonfiles to work dir.
- Run the
- Copy everything from current dir to work dir.
- Set environment var
- Expose the port
- Run the final command
We'll also create a
.dockerignore file to avoid copying over
Build An Image
Once we have the above setup ready, we can finally build an image from the Dockerfile.
docker build -t omkar/myapp:1.0 .
The above command will build a docker image and will tag it in the following format:
. at the end specifies current directory as the path to the
Run The Container
The built image can be run with the
image_id or the
docker run <image_id / tag_name> -d
Once the container runs you can visit https://localhost:8080 to see the live application in your browser.
Here's a list of a few more docker commands which you may find helpful.
List all the running containers:
Stop a container:
docker stop container_id
Remove a container:
docker rm container_id
Enter a container using interactive mode:
docker exec -it container_id bash
Note that you can find a full list of these on the docker's site. We'll see how to use docker-compose in the future.