Skip to content

Instantly share code, notes, and snippets.

@OndeVai
Last active November 17, 2018 01:20
Show Gist options
  • Save OndeVai/0a732a514134c3833bae8955f3c58c51 to your computer and use it in GitHub Desktop.
Save OndeVai/0a732a514134c3833bae8955f3c58c51 to your computer and use it in GitHub Desktop.
Docker cheatsheet
//common docker issues
http://www.pogsdotnet.com/2018/08/common-nodejs-docker-issues.html
//List all containers (only IDs)
docker ps -aq.
//Stop all running containers.
docker stop $(docker ps -aq) -f //-f is force and optional
//Remove all containers.
docker rm $(docker ps -aq) -f //-f is force and optional
//Remove all images.
docker rmi $(docker images -q) -f //-f is force and optional
//show docker containers [-a for "all" including ones not running]
docker ps [-a]
//remove container [and its volume]
docker rm [-v] "container id"
//stop a container
docker stop "container id"
//get runtime logs of a container
docker logs "container id"
//run local source code in a container (without dockerfile)
//means docker run -p [container port]:[code port] -v [current working directory]:[volume] -w "[docker working directory]" //[image name] [command]
cd /source-code-dir
docker run -p 8080:3000 -v $(pwd):/var/www -w "/var/www" node npm start
//show images
docker images -a
//delete an image
docker rmi [image id]
//dotnet core info:
//for 2.1 use microsoft/dotnet:2.1-sdk for builds and microsoft/dotnet:2.1-aspnetcore-runtime for running
//for 2.0 use microsoft/aspnetcore-build:2.0 for builds and microsoft/aspnetcore:2.0 for running
//for development, turn off httpsredirection()
//doesn't play nice with localhost so use http://0.0.0.0:5000 instead of http://localhost:5000 in launchsettings.json
//run a container in interactive mode --run command line within container
//means docker run [interactive mode] -p [external:internal] -v [local/volume] [image] [the shell program ie bash]
docker run -it -p 8080:5000 -v $(pwd):/aspcore -w "/aspcore" microsoft/dotnet:2.1-sdk /bin/bash
//then from there you get a command prompt and can run stuff ie "dotnet restore"
//sample docker file for nodejs
-------
#the image we are starting from (image:tag)
FROM node:10.13.0
#the maintainer's name
MAINTAINER Bob Dev
#environment variables
ENV NODE_ENV=production
ENV PORT=3000
#copy current directory's files to volume
COPY . /var/www
#set the working directory for run and entrypoint commands
WORKDIR /var/www
#mount a volume
#VOLUME ["/var/www"]
#tasks to run before entrypoint called
RUN npm install
#the port to expose for the container (in this case use the env variable above)
EXPOSE $PORT
#the final command to run to fire it up
ENTRYPOINT ["npm", "start"]
-----
//then run to build the image
docker build -t [image name ie "bobdev/node"]
//the fire up container from image
//meaning docker run [-p means daemon mode - no console output] -p [container port:external port]
docker run -d -p 3000:3000 bobdev/node
//sample dockerfile for asp.net core
------
#get the base image for runtime, expose port 80
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
#get the base image for building
FROM microsoft/dotnet:2.1-sdk AS build
#change to /src (on container)
WORKDIR /src
#copy csproj from local directory to /src directory
COPY ["aspnetcore-site.csproj", "./"]
#restore nuget packages for copied csproj
RUN dotnet restore "./aspnetcore-site.csproj"
#copy the rest of the local files over to /src
COPY . .
#change to within /src directory
WORKDIR "/src/."
#build the csproj with the other files copied in to /app
RUN dotnet build "aspnetcore-site.csproj" -c Release -o /app
#add another layer on top of build
FROM build AS publish
#publish to /app
RUN dotnet publish "aspnetcore-site.csproj" -c Release -o /app
#copy files from publish and run app with the entry point
FROM base AS final
#ENV ASPNETCORE_URLS=http://*:5000 //if you wanted to change the port that the webapp is running under
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "aspnetcore-site.dll"]
------
//then run to build the image
docker build -t [image name ie "bobdev/aspnetcore"]
//the fire up container from image
//meaning docker run [-p means daemon mode - no console output] -p [container port:external port]
docker run -d -p 8080:80 bobdev/aspnetcore
//for docker compose examples see projects source code
//docker exec : to run commands on a running container, mainly terminal into a running container
docker exec -it [container id] /bin/bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment