Copy with Dockerfile - docker

I try to run the Angular app inside docker with Nginx:
$ ls
dist Dockerfile
Dockerfile:
FROM nginx
COPY ./dist/statistic-ui /usr/share/nginx/html/
Inside dist/statistic-ui/ all app files.
But COPY command doesn't work, Nginx just starts with default welcome page and when I check files inside /usr/share/nginx/html/ only default Nginx files.
Why COPY command doesn't work and how to fix it?
UPDATE
Run docker container
sudo docker run -d --name ui -p 8082:80 nginx

You need to build an image from your Dockerfile then run a container from that image:
docker build -t angularapp .
docker run -d --name ui -p 8082:80 angularapp
Make sure you include the trailing dot at the end of the docker build command.

Related

Folder created in Dockerfile not visible after running container

I am using Docker and have a docker-compose.yml and a Dockerfile. In my Dockerfile, I create a folder. When I build the container, I can see that the folder is created, but when I run the container, I can see all the files, but the folder that I created during the container build is not visible.
Both of these files are in the same location.
Here is docker-compose
version: '3'
services:
app:
build: .
volumes:
- ./:/app
command: tail -f /dev/null #command to leave the container on
Here is my Dockerfile
FROM alpine
WORKDIR /app
COPY . /app
RUN mkdir "test"
RUN ls
To build the container I use the command: docker-compose build --progress=plain --no-cache. Command RUN ls from Dockerfile prints me that there are 3 files: Dockerfile, docker-compose.yml and created in Dockerfile test directory.
When my container is running and i'm entering the docker to check which files are there i haven't directory 'test'.
I probably have 'volumes' mounted incorrectly. When I delete them, after entering the container, I see the 'test' folder. Unfortunately,
I want my files in the container and on the host to be sync.
This is a simple example. I have the same thing creating dependencies in nodejs. I think that the question written in this way will be more helpful to others.
When you mount the same volume in docker-compose it will mount that folder to the running image, and test folder will be overwritten by mounted folder.
This may work for you (to create folder from docker-compose file), but im not really sure in your use case:
version: '3'
services:
app:
build: .
volumes:
- ./:/app
command: mkdir /app/test && tail -f /dev/null
Based on your comment, below is an example on how i would use Dockerfile to build node packages and save node_modules back to host:
Dockerfile
FROM node:latest
COPY . /app
RUN npm install
Sh
#!/bin/bash
docker build -t my-image .
container_id=$(docker run -d my-image)
docker cp $container_id:/app/node_modules .
docker stop $container_id
docker rm $container_id
Or more simple way, on how I use to do it, is just to run the docker image and ssh into it:
docker run --rm -it -p 80:80 -v $(pwd):/home/app node:14.19-alpine
ssh into running container, perform npm commands then exit;

docker container stops after docker run

I have a docker file which when built and run stops. I am trying to run both client and server in one docker container. If there is any solution to use docker-compose, then that is already in place and working fine. Please advise how to keep the container up and running using docker run. Thanks!
Here is my docker file, package.json and screenshot of folder structure.
DockerFile contents:
FROM node:14.14.0-alpine
RUN apk update && apk add bash
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
WORKDIR /app
EXPOSE 3000
EXPOSE 4565
CMD ["npm","run","prebuild"]
docker build: command:
docker build -t sample .
docker run command:
docker run -d -it --name sm -v `pwd`:/app sample
Package.json:

How to copy HTML onto index.html in docker

Here is my docker file I was wondering how can I copy the local html code which is in the same directory as my dockerfile. This command somehow did not work as when I ran a curl on my docker IP
it was not my html code it was the default code.
FROM httpd
EXPOSE 80
COPY public-html.html /usr/local/apache2/htdocs/
The problem is that you built the image, but did not run it.
Example to run it by mounting the file as a volume into the container:
docker run -it --rm -d -p 8080:80 --name web -v ./public-html.html:/usr/share/nginx/html nginx
Now you can do a curl http://localhost:8080.
Example for building it:
FROM nginx:latest
COPY ./public-html.html /usr/share/nginx/html/index.html
More information here

executing a simple go .exe in a dockerfile

I have the following dockerfile and everything works fine except for running the .exe
FROM golang:latest
# Set the Current Working Directory inside the container
WORKDIR $GOPATH/src/github.com/user/goserver
# Copy everything from the current directory to the PWD (Present Working Directory) inside the container
COPY . .
# Download all the dependencies
RUN go get -d -v ./...
# Install the package
RUN GOOS=linux GOARCH=amd64 go build -o goserver .
# This container exposes port 8080 to the outside world
EXPOSE 8080
# Run the executable
CMD ./goserver
The problem is that it does not execute './goserver'. I need to manually go into the container and then execute it. Any idea what could be going wrong here ?
The problem is the way you are running the container.
By running the container with the following:
docker run -it -p 8080:8080 goserver /bin/bash
you are overriding the command defined with CMD in Dockerfile to bin/bash command.
You can start the container in detached mode by running it as:
docker run -d -p 8080:8080 goserver
Further, if you want to later exec into the container then you can use the docker exec command.

Copy files from host to container when container starts on any host

I want to copy files from host to Docker container when I run the container on any host.
here is my Dockerfile
FROM tomcat:9
EXPOSE 8080
ADD ./target/app.war /tmp/myapp.war
RUN unzip /tmp/myapp.war -d /usr/local/tomcat/webapps/myapp
ENTRYPOINT ["cp", "-r", "/data/*", "/usr/local/tomcat/webapps/myapp/data"]
After building the docker image
docker build -t myappimage .
I am running it with:
docker run --mount type=bind,source=d:/data,destination=/data --rm -it -p 8081:8080 myappimage
but this throws error cp: cannot stat '/data/*': No such file or directory
I am not sure why mounting is not working, it should copy all files from my host directory d:/data to Docker container directory /data when a container starts.
This command in ENTRYPOINT is run in Docker container.
You can try:
FROM tomcat:9
EXPOSE 8080
ADD ./target/app.war /tmp/myapp.war
RUN unzip /tmp/myapp.war -d /usr/local/tomcat/webapps/myapp
COPY /data /usr/local/tomcat/webapps/myapp/data/
I hope /usr/local/tomcat/webapps/myapp/data directory exist in image prior to copying. The command seems to working fine on my machine (Mac). Not sure if it's the d:/ that is causing the issue.
Also you can try using the -v option with a z flag (It solved the same issue for me), assuming you are inside d: directory
docker run -v "$(pwd)"/data:/data:z --rm -it -p 8081:8080 myappimage
With -v it will create an endpoint for you. You can read here

Resources