How to run docker container on your local machine

Reading Time: 2 minutes

Introduction

I have been working on a hobby project, Nutrition Tracking App, to practice the latest Continuous Integration tools. This post is about dockerizing an Angular2 app and using in your local machine. I skipped the basic definitions about Docker, Containers and CI for the sake of simplicity.

Docker Commands

Frequently used docker commands.

Build docker an image

docker build -t image-name .

Run docker an image

docker run -p 80:80 -it image-name

Stop all docker containers

docker stop $(docker ps -a -q)

Remove all docker containers

docker rm $(docker ps -a -q)

Remove all docker images

docker rmi $(docker images -q)

Port bindings of a specific container

docker inspect [container-id]

Pull an image from Docker Hub 

docker pull sy456/angular2webpack -->sy456 is my account name at dockerhub

Push an image to Docker Hub 

docker push sy456/angular2webpack -->sy456 is my account name at dockerhub

Dockerfile

Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Simple dockerfile that copies “dist” folder to /usr/share/nginx/html

FROM nginx
COPY dist /usr/share/nginx/html
EXPOSE 80

Project Structure

Dockerfile should be located in the root of the project. Here is an example from my project.

How to run docker container on your local machine

Build 

docker build -t angular-webpack .

Run

docker run -p 9000:80 -t angular-webpack

This will create a container with the image ‘angular-webpack’ and bind the container’s port 80 to the host machine’s port 9000. After “docker run -p 9000:80 -it angular-webpack” command, docker container runs on Linux virtual machine. So we can’t run Docker natively on Windows or a Mac. Following properties must be set.

Set environment properties

set USERPROFILE = C:\Users\xxx  --> set your user proile 

set DOCKER_CERT_PATH=%USERPROFILE%\.docker\machine\machines\default

set DOCKER_HOST=tcp://192.168.99.100 --> find this IP in Docker Quick Start terminal

set DOCKER_MACHINE_NAME=default

set DOCKER_TLS_VERIFY=1

Test

http://192.168.99.100:9000/index.html

For Troubleshooting

https://stackoverflow.com/questions/41208782/docker-localhost-process-not-working-on-windows

 

One thought on “How to run docker container on your local machine

Leave a Reply

Your email address will not be published. Required fields are marked *