BUSYBOX script is wrong - docker

I need to start script in busybox container which will outuput the date and words the busybox is running
when I'm up my compose file i just see that:
busybox_1 | tail: invalid number 'sh ./5sec.sh'
This is my script:
while true; do
sleep 5
date
echo busybox is running
done
It's my Dockerfile:
FROM busybox:noauto
COPY /5sec.sh /5sec.sh
RUN chmod 777 5sec.sh
CMD ./5sec.sh
It's my compose file (just in case) :
version: '3'
services:
nginx:
image: "nginx:latest"
env_file: .env
ports:
- $HTTP_PORT:80
volumes:
- nginx-vol:/var/log/nginx
busybox:
image: "busybox:noauto"
volumes:
- nginx-vol:/var/log/nginx
volumes:
nginx-vol:
Help me please. How to start script automaticly. (Sorry for bad English)

I don't know what is this docker image busybox:noauto (probably your local image - build by you), and I guess this is reason of your problem. It's look like this image have some RUN command with tail or something like it.
I propose to use some standard busybox from dockerhub for your base image, for example busybox:1:
FROM busybox:1
COPY /5sec.sh /5sec.sh
RUN chmod 777 5sec.sh
CMD ./5sec.sh
Second question you should use build instead of image in you docker-compose.yaml if you want build image by yourself from your Dockerfile:
version: '3'
services:
nginx:
image: "nginx:latest"
env_file: .env
ports:
- $HTTP_PORT:80
volumes:
- ./nginx-vol:/var/log/nginx
busybox:
build: .
volumes:
- ./nginx-vol:/var/log/nginx
This should solve your problem.
Notes:
chmod 777 isn't a good practice
script should start with Shebang - #!/bin/sh in your case

Related

Docker container is running without port mapping

I created a docker-compose file to build image from dockerfile and then run container this is my code:
Dockerfile
FROM anapsix/alpine-java
VOLUME [ "/var/run/jars/" ]
ADD hello-world.jar /var/run/jars/
EXPOSE 8080
ENTRYPOINT [ "java" ]
CMD ["-?"]
docker-compose.yml
version: '3'
services:
hello-world-image:
build: .
image: hello-world-image
hello-world:
image: hello-world-image
container_name: hello-world
ports:
- "8080:8080"
volumes:
- ./logs_ACM:/root/logs_ACM
command: -jar /var/run/jars/hello-world.jar
restart: always
docker ps output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
103b0a3c30e3 hello-world-image "java -jar /var/run/…" 5 seconds ago Restarting (1) Less than a second ago hello-world
When i check running containers with "docker ps" the port column is empty therefore no port mapping was done even though i specified ports in my docker compose file.
What changes needed to be done on my docker-compose file to solve this issue ?
new version of dockerfile and docker-compose :
FROM anapsix/alpine-java
USER root
RUN mkdir -p /var/run/jars/
COPY spring-petclinic-2.4.2.jar /var/run/jars/
EXPOSE 8081
ENTRYPOINT [ "java" ]
CMD ["-?"]
version: '3' # '3' means '3.0'
services:
spring-petclinic:
build: .
# Only if you're planning to `docker-compose push`
# image: registry.example.com/name/hello-world-image:${TAG:-latest}
ports:
- "8081:8081"
volumes:
# A bind-mount directory to read out log files is a good use of
# `volumes:`. This does not require special setup in the Dockerfile.
- ./logs_ACM:/root/logs_ACM
command: -jar /var/run/jars/spring-petclinic-2.4.2.jar
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_ALLOW_EMPTY_PASSWORD=true
- MYSQL_USER=petclinic
- MYSQL_PASSWORD=petclinic
- MYSQL_DATABASE=petclinic
volumes:
- "./conf.d:/etc/mysql/conf.d:ro"
I think your single biggest problem here is the VOLUME directive in the Dockerfile. The Dockerfile documentation for VOLUME notes:
Changing the volume from within the Dockerfile: If any build steps change the data within the volume after it has been declared, those changes will be discarded.
So when you declare a VOLUME for the directory containing the jar file, and then try to ADD content to it, it gets lost.
In most practical cases you don't need a VOLUME. You should be able to rewrite the Dockerfile to:
FROM anapsix/alpine-java
# Do not create a VOLUME.
# Generally prefer COPY to ADD. Will create the target directory if needed.
COPY hello-world.jar /var/run/jars/
EXPOSE 8080
# Don't set an ENTRYPOINT just naming an interpreter.
# Do make the default container command be to run the application.
CMD ["java", "-jar", "/var/run/jars/hello-world.jar"]
In the docker-compose.yml file, you don't need a separate "service" just to build the image, and you shouldn't typically need to override container_name: (provided by Compose) or command: (from the Dockerfile). This could be reduced to:
version: '3.8' # '3' means '3.0'
services:
hello-world:
build: .
# Only if you're planning to `docker-compose push`
# image: registry.example.com/name/hello-world-image:${TAG:-latest}
ports:
- "8080:8080"
volumes:
# A bind-mount directory to read out log files is a good use of
# `volumes:`. This does not require special setup in the Dockerfile.
- ./logs_ACM:/root/logs_ACM
# Don't enable auto-restart until you've debugged the start sequence
# restart: always

How to apply the same volumes to multiple docker compose services?

Let's suppose there are two services and they have several volumes defined. But most of those volumes are used on both services:
version: '3'
services:
service1:
image: node:lts-alpine
working_dir: /
volumes:
- ./package.json:/package.json
- ./tsconfig.json:/tsconfig.json
- ./packages:/packages
- ./node_modules:/node_modules
- ./services/service1:/services/service1
command: yarn service1:start
service2:
image: node:lts-alpine
working_dir: /
volumes:
- ./package.json:/package.json
- ./tsconfig.json:/tsconfig.json
- ./packages:/packages
- ./node_modules:/node_modules
- ./services/service2:/services/service2
command: yarn service2:start
Is there a way to prevent this duplication?
I would love to do something like this:
version: '3'
services:
service1:
image: node:lts-alpine
working_dir: /
volumes:
- myVolumeList
- ./services/service1:/services/service1
command: yarn start
service2:
image: node:lts-alpine
working_dir: /
volumes:
- myVolumeList
- ./services/service2:/services/service2
command: yarn start
myVolumeList:
- ./package.json:/package.json
- ./tsconfig.json:/tsconfig.json
- ./packages:/packages
- ./node_modules:/node_modules
Edit: I use docker compose for local development only. Volumes are great for me because changing source code files will automatically restart my services. Thus copying files once isn't enough
The code for your application should generally be in a Docker image. You can launch multiple containers from the same image, possibly with different command:. For example, you might write a Dockerfile like:
FROM node:lts-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install
COPY ./ ./
CMD yarn start
Having described this image, you can reference it in the docker-compose.yml, overriding the command: for each service:
version: '3'
services:
service1:
build: .
command: 'yarn service1:start'
service2:
build: .
command: 'yarn service2:start'
(Compose will probably try to build a separate image for each service, but because of Docker layer caching, "building" the service2 image will run very quickly and wind up with a second tag on the same image.)
This setup needs no bind-mounts at all, and if you push the built images to a Docker registry, you can run them on a system without the application code or even Node available.
Natively, you can do:
Maybe this solve your problem.
version: "3"
services:
srv1:
image: someimage
volumes:
- data:/data
srv2:
image: someimage
volumes:
- data:/data
volumes:
data:
There's a plugin - https://github.com/MatchbookLab/local-persist (read it before use!)- that let you change the volume mountpoint.
Basicaly, install it: curl -fsSL https://raw.githubusercontent.com/MatchbookLab/local-persist/master/scripts/install.sh | sudo bash
Then create a volume:
docker volume create -d local-persist -o mountpoint=/data/images --name=images
Then use as many containers as you want:
docker run -d -v images:/path/to/images/on/one/ one
docker run -d -v images:/path/to/images/on/two/ two
If you whant to use docker-compose, there's a example:
version: '3'
services:
one:
image: alpine
working_dir: /one/
command: sleep 600
volumes:
- data:/one/
two:
image: alpine
working_dir: /two/
command: sleep 600
volumes:
- data:/two/
volumes:
data:
driver: local-persist
driver_opts:
mountpoint: /data/local-persist/data
Almost the same question here: docker volume custom mount point
This only work on docker-compose version '2':
version: '2'
services:
srv1:
image: sometag
volumes_from:
- data
srv2:
image: sometag
volumes_from:
- data
data:
image: sometag
volumes:
- ./code-in-host:/code

docker-compose and using local image with mounted volume

I have an image I create with Dockerfile
FROM mhart/alpine-node:latest
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY src /app
Now in docker-compose.yml I build this image
version: '3.7'
services:
enginetonic:
build:
context: .
image: enginetonic:compose
mongodb:
image: mongo:latest
container_name: 'mongodb'
ports:
- 27017:27017
restart: always
monitor-service:
image: enginetonic:compose
container_name: monitorService
command: nodemon monitor/monitor.js
restart: on-failure
#common services
access-token-service:
image: enginetonic:compose
container_name: accessTokenService
command: nodemon service/access-token-service/access-token-service.js
restart: on-failure
depends_on:
- mongodb
In all documentation to bind:mount or use volumes I found, it is used with other docker commands
example
$ docker service create \
--mount 'type=volume,src=<VOLUME-NAME>,dst=<CONTAINER-PATH>,volume-driver=local,volume-opt=type=nfs,volume-opt=device=<nfs-server>:<nfs-path>,"volume-opt=o=addr=<nfs-address>,vers=4,soft,timeo=180,bg,tcp,rw"'
--name myservice \
<IMAGE>
How to use volumes, so that every service that covers the whole /src/ directory, so that every service I start with nodemon reflects the files changed in the whole source code?
I would do a volume map in docker-compose.yml like this:
volumes:
- ./app/monitor:/path/to/your/workdir/monitor
And adjust the command to use file monitor, like nodemon, to restart service when there is any file changes:
command: ["nodemon", "/path/to/your/workdir/monitor/monitor.js"]
You may need to adjust the nodemon arguments or configs based on what you need.
PS. you do not need to tag/push your image. Simply build it directly in docker-compose#build

Docker container exited with code 0 after docker-compose up -d

I know the question has been asked in various situations, but I'm still stucked despite everything I read on the Internet.
I want to have a script executed after the container "mywebsite" is built and I used ENTRYPOINT for that and I know that in normal use, after the ENTRYPOINT command is executed, the container "mywebsite" exit. I tried several tricks to avoid exit, unfortunately without success.
In my DOCKERFILE I have this :
FROM php:7.1.17-apache
[...]
WORKDIR /var/www
COPY docker-entrypoint.sh /var/www/docker-entrypoint.sh
ENTRYPOINT ["sh", "/var/www/docker-entrypoint.sh"]
Then in my docker-entrypoint.sh I have this :
#!/bin/bash
set -e
cd www
chown -R www-data:www-data sites modules themes
exec "$#"
And here is my docker-compose.yml :
version: '3.3'
services:
mywebsite:
build: .
extra_hosts:
- "mywebsite.local:127.0.0.1"
hostname: mywebsite
domainname: local
ports:
- 8088:80
volumes:
- ./www:/var/www/www
- ./vendor:/var/www/vendor
- ./scripts:/var/www/scripts
links:
- database:database
restart: always
tty: true
database:
image: mysql:5.5.49
container_name: mysql-container
ports:
- 3307:3306
volumes:
- ./www/dumps/mywebsite.sql:/docker-entrypoint-initdb.d/dump.sql
restart: always
command: --max_allowed_packet=32505856
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mywebsite
When build, all steps are fine, and everything is set properly, but the container "mywebsite" keep exiting. (The "database" service is running fine)
I haded tty: true and exec "$#" but none of that works.
You can end with command like tail -f /dev/null
I often use this directly in my docker-compose.yml with command: tail -f /dev/null. And it is easy to see how I keep the container running.
I had the same problem when creating my own image from a postgis-image. The problem was that I added an entrypoint. When I removed the entrypoint, build the image again, docker-compose does start my container and postgis was accepting connections.
dockerfile
FROM postgis/postgis:12-master
COPY organisation.sql
#ENTRYPOINT ["docker-entrypoint.sh"] #This was the problem
In docker-compose I did not need command's or tty.
version: "3.7"
services:
mydb:
image: mydb:latest
container_name: mytest
ports:
- "5432:5432"
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret

Docker-compose volumes doesn't copy any files

I'm in Fedora 23 and i'm using docker-compose to build two containers: app and db.
I want to use that docker as my dev env, but have to execute docker-compose build and up every time i change the code isn't nice. So i was searching and tried the "volumes" option but my code doesn't get copied to docker.
When i run docker-build, a "RUN ls" command doesn't list the "app" folder or any files of it.
Obs.: in the root folder I have: docker-compose.yml, .gitignore, app (folder), db (folder)
Obs¹.: If I remove the volumes and working_dir options and instead I use a "COPY . /app" command inside the app/Dockerfile it works and my app is running, but I want it to sync my code.
Anyone know how to make it work?
My docker-compose file is:
version: '2'
services:
app:
build: ./app
ports:
- "3000:3000"
depends_on:
- db
environment:
- DATABASE_HOST=db
- DATABASE_USER=myuser
- DATABASE_PASSWORD=mypass
- DATABASE_NAME=dbusuarios
- PORT=3000
volumes:
- ./app:/app
working_dir: /app
db:
build: ./db
environment:
- MYSQL_ROOT_PASSWORD=123
- MYSQL_DATABASE=dbusuarios
- MYSQL_USER=myuser
- MYSQL_PASSWORD=mypass
Here you can see my app container Dockerfile:
https://gist.github.com/jradesenv/d3b5c09f2fcf3a41f392d665e4ca0fb9
Heres the output of the RUN ls command inside Dockerfile:
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
A volume is mounted in a container. The Dockerfile is used to create the image, and that image is used to make the container. What that means is a RUN ls inside your Dockerfile will show the filesystem before the volume is mounted. If you need these files to be part of the image for your build to complete, they shouldn't be in the volume and you'll need to copy them with the COPY command as you've described. If you simply want evidence that these files are mounted inside your running container, run a
docker exec $container_name ls -l /
Where $container_name will be something like ${folder_name}_app_1, which you'll see in a docker ps.
Two things, have you tried version: '3' version two seems to be outdated. Also try putting the working_dir into the Dockerfile rather than the docker-compose. Maybe it's not supported in version 2?
This is a recent docker-compose I have used with volumes and workdirs in the respective Dockerfiles:
version: '3'
services:
frontend:
build:
context: ./frontend
dockerfile: Dockerfile.dev
ports:
- 3001:3001
volumes:
- ./frontend:/app
networks:
- frontend
backend:
build: .
ports:
- 3000:3000
volumes:
- .:/app
networks:
- frontend
- backend
depends_on:
- "mongo"
mongo:
image: mongo
volumes:
- ./data/db:/data/db
ports:
- 27017:27017
networks:
- backend
networks:
frontend:
backend:
You can extend or override docker compose configuration. Please follow for more info: https://docs.docker.com/compose/extends/
I had this same issue in Windows!
volumes:
- ./src/:/var/www/html
In windows ./src/ this syntax might not work in regular command prompt, so use powershell instead and then run docker-compose up -d.
it should work if it's a mounting issue.

Resources