Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Run a Docker Container on Your Local Machine

DZone's Guide to

How to Run a Docker Container on Your Local Machine

This quick walkthrough will show you how to dockerize and run an Angular2 app in a container on your local machine, with frequently used Docker commands.

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

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 on 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 Image

docker build -t image-name .

Run Docker 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]

Dockerfile

Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Here's a simple Dockerfile that copies the "dist" folder to /usr/share/nginx/html:

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

Project Structure

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

How to Run the 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 the "docker run -p 9000:80 -it angular-webpack" command, the Docker container runs on a Linux virtual machine. So we can't run Docker natively on Windows or a Mac. The 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.

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:
docker ,containers ,devops

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}